// pages/fin/fin.js
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page_name: "page_fini",
    full_screen_color: app.globalData.full_screen_color,
    front_color: app.globalData.front_color,
    desc: "感谢您的参与，2s后自动跳转至下个实验",
    over: 0,
    timer: 2,
    show_task_desc: 0,
    title: '总结',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.pre_load()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  pre_load() {
    var that = this
    var remote_ip = app.globalData.remote_ip
    wx.request({
      url: remote_ip + this.data.page_name,
      header: {
        'content-type': 'application/json',
        "uid": app.globalData.uid,
      },
      success(res) {
        const rs = res.data.data[0];

        // 设置全局数据
        getApp().globalData.full_screen_color = rs.bg_color;
        getApp().globalData.front_color = rs.front_color;
        getApp().globalData.title = rs.title;

        that.setData({
          'full_screen_color': rs.bg_color,
          'front_color': rs.front_color,
          'desc': rs.desc,
          'over': rs.over, 
          'timer': rs.timer,
          'title': rs.title,
          'show_task_desc': rs.show_task_desc,
        });
        // 设置页面颜色
        app.page_color_init();
        that.handle_next_event()

      }
    })
  },

  handle_next_event() {
    var url_link = "/pages/eye_calibration/eye_calibration"
    var f = this.handle_next_event_v1
    if (this.data.over === 1 || this.data.timer <= 0) {
      f = this.handle_next_event_v2
    } else if (this.data.show_task_desc == 1) {
      f = this.handle_next_event_v1
      url_link = "/pages/ready/ready"
    }

    var timeout = this.data.timer;
    var timer_id = setTimeout(function () {
      f(url_link);
      clearTimeout(timer_id);
    }, timeout)

  },

  handle_next_event_v1(url_link) {
    var remote_ip = app.globalData.remote_ip
    wx.request({
      url: remote_ip + 'mode_change',
      header: {
        'content-type': 'application/json',
        "uid": app.globalData.uid,
      },
      success(res) {
        if (res.data.code != 0) {
          return;
        }
        wx.redirectTo({
          url: url_link,
        })
      }
    })
  },

  handle_next_event_v2(url_link) {
    wx.redirectTo({
      url: '/pages/select_mode/select_mode',
    })
  },

})


